package com.snap.awesomeserial.utils;

import android.graphics.Paint;

/**
 * Created by QianChao on 2016/4/27.
 */
public class GetTextWidthUtil {
    /**
     * 获取所有文本的宽度
     *
     * @param paint
     * @param str
     * @return
     */
    public static int getTextWidth(Paint paint, String str) {
        int iRet = 0;
        if (str != null && str.length() > 0) {
            int len = str.length();
            float[] widths = new float[len];
            paint.getTextWidths(str, widths);
            for (int j = 0; j < len; j++) {
                iRet += (int) Math.ceil(widths[j]);
            }
        }
        return iRet;
    }

    /**
     * 获取单个文本的宽度
     *
     * @param paint
     * @param str
     * @return
     */
    public static int getSingleTextWidth(Paint paint, String str) {
        int iRet = 0;
        if (str != null && str.length() > 0) {
            float[] widths = new float[1];
            paint.getTextWidths(str, widths);
            iRet += (int) Math.ceil(widths[0]);
        }
        return iRet;
    }


}
